package com.example.ceramics.database.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.ceramics.database.DatabaseContants;
import com.example.ceramics.database.DatabaseHelper;
import com.example.ceramics.database.dao.IUserDao;
import com.example.ceramics.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {

    private final DatabaseHelper databaseHelper;

    public UserDaoImpl(Context context) {
        databaseHelper = new DatabaseHelper(context);
    }

    @Override
    public int addUser(UserEntity user) {
        if (hasAccount(user.getAccount())) {
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "insert into " + DatabaseContants.TABLE_USER +
                "(account,password,name,nickname,phone,email,addressProvince,addressCity,addressCounty,addressDetail,img)" +
                " values(?,?,?,?,?,?,?,?,?,?,?)";
        db.execSQL(sql, new Object[]{user.getAccount(), user.getPassword(), user.getName(),
                user.getNickname(), user.getPhone(), user.getEmail(), user.getAddressProvince(),
                user.getAddressCity(), user.getAddressCounty(), user.getAddressDetail(),user.getImg()});
        db.close();
        return 1;
    }

    @Override
    public int deleteUser(String account) {
        if(!hasAccount(account)){
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "delete from "+DatabaseContants.TABLE_USER+" where account=?";
        db.execSQL(sql, new Object[]{account});
        db.close();
        return 1;
    }

    @Override
    public int updateAddress(UserEntity user) {
        if(!hasAccount(user.getAccount())){
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "update "+DatabaseContants.TABLE_USER+" set name=?,phone=?," +
                "addressProvince=?,addressCity=?,addressCounty=?,addressDetail=? where account=?";
        db.execSQL(sql,new Object[]{user.getName(), user.getPhone(), user.getAddressProvince(),user.getAddressCity(),
                user.getAddressCounty(),user.getAddressDetail(),user.getAccount()});
        db.close();
        return 1;
    }

    @Override
    public int updateInfo(UserEntity user) {
        if(!hasAccount(user.getAccount())){
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "update "+DatabaseContants.TABLE_USER+" set nickname=?,phone=?," +
                "email=?,img=? where account=?";
        db.execSQL(sql,new Object[]{user.getNickname(), user.getPhone(), user.getEmail(),user.getImg(),user.getAccount()});
        db.close();
        return 1;
    }

    @Override
    public boolean hasAccount(String account) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select account from " + DatabaseContants.TABLE_USER + " where account=?";
        Cursor cursor = db.rawQuery(sql, new String[]{account});
        if (cursor.moveToFirst()) {
            cursor.close();
            db.close();
            return true;
        }
        cursor.close();
        db.close();
        return false;
    }

    @Override
    public List<UserEntity> listAllUser() {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from "+DatabaseContants.TABLE_USER;
        Cursor cursor = db.rawQuery(sql, null);
        List<UserEntity> users = new ArrayList<>();
        while (cursor.moveToNext()){
            users.add(cursorToUser(cursor));
        }
        cursor.close();
        db.close();
        return users;
    }

    private UserEntity cursorToUser(Cursor cursor) {
        String account = cursor.getString(cursor.getColumnIndex("account"));
        String password = cursor.getString(cursor.getColumnIndex("password"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String nickname = cursor.getString(cursor.getColumnIndex("nickname"));
        String phone = cursor.getString(cursor.getColumnIndex("phone"));
        String email = cursor.getString(cursor.getColumnIndex("email"));
        String addressProvince = cursor.getString(cursor.getColumnIndex("addressProvince"));
        String addressCity = cursor.getString(cursor.getColumnIndex("addressCity"));
        String addressCounty = cursor.getString(cursor.getColumnIndex("addressCounty"));
        String addressDetail = cursor.getString(cursor.getColumnIndex("addressDetail"));
        String img = cursor.getString(cursor.getColumnIndex("img"));
        UserEntity userEntity;
        userEntity = new UserEntity(account,password,name,nickname,phone,email,addressProvince,addressCity,addressCounty,addressDetail,img);
        return userEntity;
    }

    @Override
    public boolean login(String account, String password) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select password from "+DatabaseContants.TABLE_USER +" where account=?";
        Cursor cursor = db.rawQuery(sql, new String[]{account});
        if (cursor.moveToNext()) {
            String passwordActual = cursor.getString(cursor.getColumnIndex("password"));
            if (password.equals(passwordActual)) {
                cursor.close();
                db.close();
                return true;
            }
        }
        cursor.close();
        db.close();
        return false;
    }

    @Override
    public UserEntity getUser(String account) {
        SQLiteDatabase db =databaseHelper.getReadableDatabase();
        String sql = "select * from "+DatabaseContants.TABLE_USER+" where account=?";
        Cursor cursor = db.rawQuery(sql, new String[]{account});
        UserEntity user = null;
        if (cursor.moveToNext()) {
            user = cursorToUser(cursor);
        }
        db.close();
        return user;
    }
}
